﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using QingzimaLogicModel;
using Jacaranda.Model;
using QingzimaDB;

namespace QingzimaScenario
{
    public class DingdanScenario
    {
        public static DingdanLM HuoquByID(int id)
        {
            return ConvertToLM(Database.SearchOne<DingdanDB>(new AbstractProperty("ID") == id));
        }

        public static DingdanLM HuoquByKey(string key)
        {
            return ConvertToLM(Database.SearchOne<DingdanDB>(new AbstractProperty("Key") == key));
        }

        public static List<DingdanLM> HuoquModels()
        {
            List<DingdanLM> tempLMList = new List<DingdanLM>();
            foreach (DingdanDB dbModel in Database.Select<DingdanDB>("Select * From DingdanDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }
        public static IEnumerable<DingdanLM> GetDingdanList(int pageSize, int pageIndex)
        {
            return Database.Search<DingdanDB>(new AbstractProperty("ID") != null, new SortCriteria().OrderByDesc("CreatedTime"), new SearchPageCriteria { PageIndex = pageIndex, PageSize = pageSize }).Select<DingdanDB, DingdanLM>(a => new DingdanLM(a));
        }

        public static void Tianjia(DingdanLM DingdanLM)
        {
            DingdanLM.Save();
        }

        public static void Shanchu(string key)
        {
            Database.Delete<DingdanDB>(new AbstractProperty("Key") == key);
        }

        private static DingdanLM ConvertToLM(DingdanDB dbModel)
        {
            return dbModel == null ? null : new DingdanLM(dbModel);
        }
    }
}
